package cn.homework.web.servlet;

import cn.homework.domain.OrderDetails;
import cn.homework.service.ShoppingCarService;
import cn.homework.service.impl.ShoppingCarServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author ACT
 */
@WebServlet("/shoppingCartServlet")
public class ShoppingCartServlet extends HttpServlet {
    private ShoppingCarService service = new ShoppingCarServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String demand = request.getParameter("demand");
//        System.out.println("demandCart：" + demand);
        switch (demand) {
            case "show": {
                shoppingCartShow(request, response);
                break;
            }
            case "buy":{
                buyProduct(request, response);
                break;
            }
            case "delChoose": {
                delChooseShoppingCart(request, response);
                break;
            }
            case "empty": {
                emptyShoppingCart(request, response);
                break;
            }
            case "plus": {
                plusProduct(request, response);
                break;
            }
            case "less": {
                lessProduct(request, response);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 查询所有的购物车中的商品并展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void shoppingCartShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<OrderDetails> orderAllDetails = service.findAllShoppingCart();
        HttpSession session = request.getSession();
        session.setAttribute("orderAllDetails", orderAllDetails);
        response.sendRedirect(request.getContextPath() + "/CommodityShoppingCart.jsp");
    }

    /**
     * 删除选中的购物车的商品
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void delChooseShoppingCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderId = request.getParameter("orderId");
        service.deldelChooseShoppingCartProduct(orderId);
        shoppingCartShow(request, response);
    }

    /**
     * 清空购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void emptyShoppingCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        service.emptyShoppingCart();
        shoppingCartShow(request, response);
    }

    /**
     * 添加商品到购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void buyProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
//        System.out.println("id:" + id);
        service.addProductToShoppingCart(id);
        shoppingCartShow(request, response);
    }

    /**
     * +的操作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void plusProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.plusProduct(id);
        shoppingCartShow(request, response);
    }

    /**
     * -的操作
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void lessProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.lessProduct(id);
        shoppingCartShow(request, response);
    }
}
